﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LY.Entity;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Entity.Jsons;
using LY.Interface;
using LY.Common.Utils;
using System.Web.Security;

namespace QJ.CMS.Areas.Admin.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        private IUserService _sservice;
        public UserController(IUserService sService)
        {
            _sservice = sService;
        }

        public ActionResult Index()
        {
            return View();
        }

        //用户列表
        [HttpPost]
        public JsonResult UserList(UserSearch search, FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<S_User> mList = _sservice.QueryUserList(search, view);
            JsonQTable fdata = JsonQTable.ConvertFromPagedList<S_User>(mList, colkey, colsinfo.Split(','));
            return Json(fdata);
        }

        //编辑用户
        public ActionResult UserEdit(int? id)
        {
            S_User user = null;
            if (!id.HasValue)
            {
                user = new S_User();
            }
            else
            {
                user = _sservice.GetUser(id);
            }
            return View(user);
        }
        //编辑用户 保存
        [HttpPost]
        public JsonResult UserSave(int? id, S_User user, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                string errmsg = "";
                if (!Validate(user, out errmsg))
                {
                    rMsg.Status = -1;
                    rMsg.Message = errmsg;
                    return Json(rMsg);
                }
                string ss = collection["IsStop"];
                int ret = _sservice.SaveUser(user);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Data = ret;
                }
                else if (ret == -2)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "系统中已存在相同的用户名！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

        /// <summary>
        /// 参数验证
        /// </summary>
        /// <param name="user"></param>
        /// <param name="errmsg"></param>
        /// <returns></returns>
        private static bool Validate(S_User user, out string errmsg)
        {
            errmsg = "";
            if (user == null)
            {
                errmsg = "参数错误！";
                return false;
            }

            if (string.IsNullOrEmpty(user.LoginName))
            {
                errmsg = "用户账号不能为空！";
                return false;
            }

            if (!string.IsNullOrEmpty(user.Email) && !ValidateUtil.IsEmail(user.Email))
            {
                errmsg = "邮箱格式有误！";
                return false;
            }
            return true;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UserDelete(int id, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                if (id <= 0)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "参数错误！";
                    return Json(rMsg);
                }

                int ret = _sservice.DeleteUser(id);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Data = ret;
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

        //编辑用户密码
        public ActionResult ChangePassword(int? id)
        {
            S_User user = null;
            if (!id.HasValue)
            {
                user = new S_User();
            }
            else
            {
                user = _sservice.GetUser(id);
            }
            return View(user);
        }
        //编辑用户密码 保存
        [HttpPost]
        public JsonResult ChangePasswordSave(FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                //旧密码
                string oldpwd = collection["oldpwd"];
                if (string.IsNullOrEmpty(oldpwd)) {
                    rMsg.Status = -1;
                    rMsg.Message = "请输入旧密码！";
                    return Json(rMsg);
                }
                //新密码
                string newpwd = collection["newpwd"];
                if (string.IsNullOrEmpty(newpwd))
                {
                    rMsg.Status = -1;
                    rMsg.Message = "请输入新密码！";
                    return Json(rMsg);
                }
                //确认密码
                string newpwd1 = collection["newpwd1"];
                if (string.IsNullOrEmpty(newpwd))
                {
                    rMsg.Status = -1;
                    rMsg.Message = "请输入确认密码！";
                    return Json(rMsg);
                }
                //两次输入密码是否一致
                if (!newpwd.Equals(newpwd1))
                {
                    rMsg.Status = -1;
                    rMsg.Message = "两次输入密码不一致！";
                    return Json(rMsg);
                }
    
                S_User loguser = (S_User)System.Web.HttpContext.Current.Session["LoginUseInfo"];
                if (loguser == null) {
                    rMsg.Status = -1;
                    rMsg.Message = "操作超时！";
                    return Json(rMsg);
                }
                string eumpwd = LY.Common.Utils.MD5Util.GetMD5_32(oldpwd);
              
                //判断旧密码是否正确
                if (!eumpwd.Equals(loguser.Password))
                {
                    rMsg.Status = -1;
                    rMsg.Message = "旧密码输入有误！";
                    return Json(rMsg);
                }
                string emnewpwd = LY.Common.Utils.MD5Util.GetMD5_32(newpwd);
                int ret = _sservice.UpdatePwd(emnewpwd, loguser.ID);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Data = ret;
                    //退出
                    Session["LoginUseInfo"] = null;
                    FormsAuthentication.SignOut();
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "修改失败，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            Response.Clear();
            return Json(rMsg);
        }
    }
}
